package mt.dummy;

import java.util.ArrayList;
import java.util.HashMap;

import mt.Order;
import mt.comm.implement.ServerComm;
import mt.comm.implement.ServerSideMessage;

public class ServerCommDummy implements ServerComm {

	private HashMap<String, Order> SEND_ORDERS = new HashMap<String, Order>();
	private HashMap<String, String> SEND_ERRORS = new HashMap<String, String>();
	private ArrayList<String> CONNECTED_CLIENTS = new ArrayList<String>();

	@Override
	public void start() {

	}

	@Override
	public boolean hasNextMessage() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public ServerSideMessage getNextMessage() {
		// TODO Auto-generated method stub
		return null;
	}

	//envia um erro para o nickname
	@Override
	public void sendError(String toNickname, String error) {
		SEND_ERRORS.put(toNickname, error);

	}

	//verifica se um client esta ligado ao serverComm
	@Override
	public boolean clientIsConnected(String nickname) {
		return CONNECTED_CLIENTS.contains(nickname);
	}

	//desliga um nickname do serverComm
	@Override
	public void disconnectClient(String nickname) {
		CONNECTED_CLIENTS.remove(nickname);
	}

	//envia uma order para o receiversNickname
	@Override
	public void sendOrder(String receiversNickname, Order order) {
		if(!CONNECTED_CLIENTS.contains(receiversNickname))
			connectClient(receiversNickname);
		if(!SEND_ORDERS.containsValue(order))
			SEND_ORDERS.put(receiversNickname, order);
		for(Order ord: SEND_ORDERS.values()){
			if(ord.getServerOrderID() == order.getServerOrderID()){
				SEND_ORDERS.remove(receiversNickname);
				SEND_ORDERS.put(receiversNickname, order);
			}
		}
	}

	//liga um nickname ao serverComm
	public void connectClient(String nickname){
		CONNECTED_CLIENTS.add(nickname);
	}


	//verifica se o serverComm tem a order para o nickname
	public boolean containsSendOrder(String Nickname, Order order){
		return SEND_ORDERS.get(Nickname).equals(order);

	}

	//verifica se o serverComm tem o erro para o nickname
	public boolean containsSendError(String Nickname, String error){
		return SEND_ERRORS.get(Nickname).equals(error);

	}

	@Override
	public void broadcastOrder(Order order) {
		// TODO Auto-generated method stub
		
	}


}
